<?php
/**
 * FormMultiFieldView
 *
 * @version    1.0
 * @package    samples
 * @subpackage tutor
 * @author     Pablo Dall'Oglio
 * @copyright  Copyright (c) 2006-2011 Adianti Solutions Ltd. (http://www.adianti.com.br)
 * @license    http://www.adianti.com.br/framework-license
 */
class FormMultiFieldView extends TPage
{
    private $form;
    
    /**
     * Class constructor
     * Creates the page
     */
    function __construct()
    {
        parent::__construct();
        
        // create the notebook
        $notebook = new TNotebook(530, 300);
        
        // create the form
        $this->form = new TForm;
        
        // creates the notebook page
        $table = new TTable;
        
        // add the notebook inside the form
        $this->form->add($table);
        
        // adds the notebook page
        $notebook->appendPage('Multi field component', $this->form);
        
        // create the form fields
        $multifield = new TMultiField('contacts');
        
        $subfield1  = new TEntry('name');
        $subfield2  = new TEntry('email');
        
        $multifield->setHeight(140);
        // $multifield->setClass('Contato');
        $multifield->addField('name', 'Name',  $subfield1, 200);
        $multifield->addField('email','Email', $subfield2, 200);
        
        // add a row for one field
        $row=$table->addRow();
        $row->addCell(new TLabel('Multifield object:'));
        $cell = $row->addCell( $multifield );
        
        // creates the action button
        $button1=new TButton('action1');
        // define the button action
        $button1->setAction(new TAction(array($this, 'onSave')), 'Save');
        $button1->setImage('ico_save.png');
        // add a row for the button
        $row=$table->addRow();
        $row->addCell($button1);
        
        // define wich are the form fields
        $this->form->setFields(array($multifield, $button1));
        
        // add the form inside the page
        parent::add($notebook);
    }
    
    /**
     * Simulates an save button
     * Show the form content
     */
    public function onSave($param)
    {
        $data = $this->form->getData(); // optional parameter: active record class
        
        // put the data back to the form
        $this->form->setData($data);
        
        // get the contacts
        if ($data->contacts)
        {
            $message = '';
            foreach ($data->contacts as $contact)
            {
                $message .= $contact->name . ' - ' . $contact->email . '<br>';
            }
        }
        
        // show the message
        new TMessage('info', $message);
    }
}
?>